﻿package src.components 
{
	/**
	* 
	* Reliability project
	* -----------------------
	* @author  Dmitrij Boycov
	* @mail    dmitry.boycov@gmail.com
	* @date    April 27, 2010
	* @version 1
	* 
	*/
	
	import fl.data.DataProvider;
	import flash.display.Sprite;
	import flash.events.Event;
	// astra
	import com.yahoo.astra.containers.formClasses.FormItem;	
	import com.yahoo.astra.layout.LayoutContainer;
	import com.yahoo.astra.layout.modes.BoxLayout;
	import com.yahoo.astra.layout.modes.HorizontalAlignment;
	import com.yahoo.astra.layout.modes.VerticalAlignment;
	import com.yahoo.astra.fl.controls.TabBar;
	
	public class TabsPanel extends LayoutContainer{
	
		private var _panels:Array;
		private var _labels:Array
		private var _tabIndex:Number = 0;
		private var _tabBar:TabBar;
		
		/**
		 * construct 
		 * @param	panels is array following form
		 * 		[ {label:"label1",panel:"panel1"},
		 * 	 		{label:"label2",panel:"panel2"},
		 *			  ...
		 * 		]
		 */
		public function TabsPanel(panels) {
			var box : BoxLayout = new BoxLayout();
			box.direction = "vertical";
			box.verticalGap = 10;			
			super(box);				
			this.width = 310;
			if (panels != null) {				
				try {
					this.initPanels(panels);
					this._tabBar = new TabBar();					
					this._tabBar.addEventListener( Event.CHANGE, tabBarChangeHandler )
					this._tabBar.dataProvider = new DataProvider(this._labels);					
					this._tabBar.selectedIndex = this._tabIndex;
					this.addChild(this._tabBar);					
					var whiteLine = new Sprite();
					whiteLine.graphics.lineStyle(1, 0xffffff);
					whiteLine.graphics.moveTo(0, this._tabBar.y-10);
					whiteLine.graphics.lineTo(this.width,this._tabBar.y-10);		
					this.addChild(whiteLine);
					this.setPanel(this._tabIndex);
				}catch(errObject:Error) {
					trace("Error.TabsPanel.Uncorrect parametrs.");
				}
			}				
		}
		
		private function tabBarChangeHandler(event) {
			var index:int = ( event.currentTarget as TabBar ).selectedIndex;
			if (this._tabIndex != index) {
				this._tabIndex = index;
				this.setPanel(this._tabIndex);
			}
		}
		
		private function setPanel(index) {
			if (this.numChildren > 2) {
				this.removeChildAt(2);
			}
			if (this._panels[index] != null) {				
				this.addChildAt(this._panels[index], 2);			
			}
		}
		
		
		private function initPanels(panels) {
			this._labels = new Array();
			this._panels = new Array();
			var p;
			for each(p in panels) {
				this._labels.push(p.label);
				this._panels.push(p.panel);					
			}
		}
		
	}
	
}